<template>
  <div class="home">
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div class="logo-area">
          <img src="../assets/heima.png" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse?'':'200px'">
          <div class="toggle-button" @click="togggleCollepse">|||</div>
          <!-- 侧边栏菜单区域 -->
          <el-menu
            :router=true
            :collapse-transition="false"
            :collapse="isCollapse"
            unique-opened
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409FFF"

          >
            <!-- 一级菜单 -->
            <el-submenu :index="menu.id+''" v-for="menu in menuList" :key="menu.id">
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <i :class="icons[menu.id]"></i>
                <span>{{menu.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+chi.path" v-for="chi in menu.children" :key="chi.id" @click="saveNavState('/'+chi.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{chi.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧主题 -->
        <el-main>
          <!--路由占位符-->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
// import moment from 'moment'
// import Welcome from './Welcome'
export default {
  data: function () {
    return {
      test: 1575446401,
      menuList: [],
      icons: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-shixinzhengfangxing',
        '101': 'iconfont icon-bao',
        '102': 'iconfont icon-bijiben',
        '145': 'iconfont icon-diannao'
      },
      isCollapse: false, // 折叠左侧菜单
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    // console.log(moment(new Date()).format('YYYY-MM-DD'))
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录 清空sesssionStorage
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menuList = res.data
      // console.log(this.menuList)
    },
    // 折叠左侧菜单栏
    togggleCollepse () {
      console.log('no')
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (index) {
      // console.log(index)
      window.sessionStorage.setItem('activePath', index)
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  }
}
</script>
<style lang="less" scoped>
.home {
  height: 100%;
  .home-container {
    height: 100%;
  }
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  .logo-area {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 20px;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background: #333744;
  display: flex;
  flex-direction: column;
}
.el-menu {
  width: 100%;
}
.el-main {
  background: #ededf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  text-align: center;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
